package com.cyrus.vueboot.controller.auth;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.cyrus.vueboot.common.R;
import com.cyrus.vueboot.divAnno.OpsLogAnnotation;
import com.cyrus.vueboot.error.BusinessError;
import com.cyrus.vueboot.error.CommonError;
import com.cyrus.vueboot.service.AuthorityService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @Author: Cyrus
 * @CreateDate: 2020/11/15
 */
@Slf4j
@Api(value = "身份权限服务的Controller", tags = "Authority", description = "身份权限服务API")
@RestController
@RequestMapping("/auth")
public class AuthorityController {
    @Autowired
    private AuthorityService authorityService;

    @PostMapping("/search")
    public R search(Integer page, Integer size, String key) {
        IPage pages = authorityService.search(page, size, key);
        List records = pages.getRecords();
        long total = pages.getTotal();
        long size1 = pages.getSize();
        return R.ok().data("records", records).data("total", total).data("size",size1);
    }

    @ApiOperation("更新指定用户的身份权限")
    @ApiImplicitParams({@ApiImplicitParam(name="authId",value = "最新权限身份应对的Id",required=true,dataType="Integer"),
                        @ApiImplicitParam(name="id",value = "需要修改身份权限的用户id",required=true,dataType="Integer")})
    @PostMapping("/changeAuth")
    @OpsLogAnnotation(module = "权限管理",description = "更改用户权限")
    public R changeAuth(Integer authId,Integer id) {
        try {
            authorityService.changeAuth(authId, id);
            return R.ok();
        } catch (Exception e) {
            e.printStackTrace();
            throw new BusinessError(CommonError.E_10003.getCode(), CommonError.E_10003.getMsg());
        }

    }
}
